//
//  PersonInfoVC.m
//  tcpoctest
//
//  Created by gump on 10/31/12.
//  Copyright (c) 2012 gump. All rights reserved.
//

#import "PersonInfoVC.h"
#import "WFLabel_blackShadow.h"
#import "PublicFun.h"
#import "ViewManager.h"
#import "imageTransfrom.h"
#import <QuartzCore/QuartzCore.h>

#import "giftStroageDataSource.h"
#import "GiftInfoDataSource.h"

#import "giftStroageDataInfo.h"
#import "GiftInfoDataInfo.h"

#import "sendPersonReadCoin.h"
#import "sendGiftGetStroage.h"

#import "taskDataSource.h"
#import "bigHeadImageDataSource.h"

@interface PersonInfoVC ()

@end

@implementation PersonInfoVC

- (id)init
{
    self = [super init];
    if (self)
    {
    }
    return self;
}

#pragma mark - tableView DataSource -
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 68;
    }else{
    return 44;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else if(section == 1){
        return 10;
    }else{
        return 3;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"cell%d%d",
                                       indexPath.section,
                                       indexPath.row];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] ;
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
        [cell setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell.png"]]];
        UIImageView *selectImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellpress.png"]];
        [cell setSelectedBackgroundView:selectImageView];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.imageView.image = nil;
    
    if (indexPath.section == 0) {
        if ([DataControl shareControl].personDS.uinfo != nil)
        {
            NSString *strurl = [[DataControl shareControl] SearchHeadImage:
                                [DataControl shareControl].personDS.uinfo.mail];
            if (![strurl isEqual: @""])
            {
                cell.imageView.image = [UIImage imageWithContentsOfFile:strurl];
                cell.imageView.layer.cornerRadius = 5;
                cell.imageView.layer.masksToBounds = YES;
            }else{
                cell.imageView.image = [UIImage imageNamed:@"default_head.png"];
            }
        }
        
        cell.textLabel.text = @"设置头像";
        cell.detailTextLabel.text = nil;
    }else if(indexPath.section == 1){
        if (indexPath.row == 0) {
            cell.textLabel.text = @"我的礼物";
            
            NSArray *giftarr = [NSArray arrayWithArray:
                                [[DataControl shareControl].giftStroageDS GetGSArr]];
            
            int startpos = 140;
            if (giftarr.count < 4) {
                startpos = 260 - giftarr.count * 30;
            }
            
            for (UIView *v in cell.contentView.subviews) {
                [v removeFromSuperview];
            }
            
            for (int i = 0; i < giftarr.count && i < 4; ++i) {
                giftStroageDataInfo *gsinfo = [giftarr objectAtIndex:i];
                
                UIImageView *imageview = [[UIImageView alloc] initWithFrame:
                                          CGRectMake(startpos + i * 30, 5, 30, 30)];
                
                NSString *strpath = [[DataControl shareControl].giftInfoDS searchGiftImagePath:gsinfo.strgiftid];
                
                if ([strpath isEqualToString:@""]) {
                    imageview.image = [UIImage imageNamed:@"gift_default.png"];
                }
                else
                {
                    imageview.image = [UIImage imageWithContentsOfFile:strpath];
                }
                
                [cell.contentView addSubview:imageview];
            }
        }
        else if (indexPath.row == 1) {
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.textLabel.text = [NSString stringWithFormat:@"%@号",T_PROJECTNAME];
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%d",
                                         [DataControl shareControl].personDS.uinfo.ppNumber];
        }else if (indexPath.row == 2){
            cell.textLabel.text = @"昵称";
            
            if ([DataControl shareControl].personDS.uinfo != nil)
            {
                cell.detailTextLabel.text = [DataControl shareControl].personDS.uinfo.name;
            }
            
            
        }else if (indexPath.row == 3){
            cell.textLabel.text = @"性别";
            
            if([DataControl shareControl].personDS.uinfo != nil)
            {
                cell.detailTextLabel.text = ([DataControl shareControl].personDS.uinfo.sex == 1 ? @"男":@"女");
            }
            
            
        }else if (indexPath.row == 4){
            cell.textLabel.text = @"年龄";
            
            if ([DataControl shareControl].personDS.uinfo != nil)
            {
                cell.detailTextLabel.text =
                [NSString stringWithFormat:@"%d",[DataControl shareControl].personDS.uinfo.age];
            }
            
        }else if (indexPath.row == 5){
            cell.textLabel.text = @"城市";
            
            if ([DataControl shareControl].personDS.uinfo != nil)
            {
                cell.detailTextLabel.text =
                [NSString stringWithFormat:@"%@ %@",
                 getProvinceFromMapArr([DataControl shareControl].personDS.uinfo.province),
                 getCityFromMapArr([DataControl shareControl].personDS.uinfo.province,
                                   [DataControl shareControl].personDS.uinfo.city)];
            }
            
        }else if (indexPath.row == 6){
            cell.textLabel.text = @"个性签名";
            
            if ([DataControl shareControl].personDS.uinfo != nil)
            {
                cell.detailTextLabel.text = [DataControl shareControl].personDS.uinfo.strSign;
            }
            
        }else if (indexPath.row == 7){
            cell.textLabel.text = @"我的邮箱";
            
            if ([DataControl shareControl].personDS.uinfo != nil)
            {
                cell.detailTextLabel.text = [DataControl shareControl].personDS.uinfo.mymail;
            }
            
        }
        else if(indexPath.row == 8){
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.textLabel.text = T_GLAMOUR;
            
            if ([DataControl shareControl].personDS.uinfo != nil)
            {
                cell.detailTextLabel.text = [NSString stringWithFormat:@"%d",
                                             [DataControl shareControl].personDS.uinfo.iGlamour];
            }
            
        }else if(indexPath.row == 9){
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.textLabel.text = @"金币";
            
            if ([DataControl shareControl].personDS.uinfo != nil)
            {
                
                float fprice = [[DataControl shareControl].personDS.uinfo.strcoin floatValue];
                int iprice = fprice / 10000;
                if (iprice > 0) {
                    cell.detailTextLabel.text = [NSString stringWithFormat:@"%d万",iprice];
                }
                else
                {
                    cell.detailTextLabel.text = [DataControl shareControl].personDS.uinfo.strcoin;
                }
            }
            
        }
        
    }else if(indexPath.section == 2){
        cell.detailTextLabel.text = nil;
        if (indexPath.row == 0) {
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.detailTextLabel.text = nil;
            cell.textLabel.text = @"拒绝未关注人信息";
            if (!CheckSwitch) {
                CheckSwitch = [[ UISwitch alloc]initWithFrame:CGRectMake(190,15,0.0,0.0)];
                [CheckSwitch addTarget: self action:@selector(CheckswitchValueChanged:)
                      forControlEvents:UIControlEventValueChanged];
                CheckSwitch.on = [DataControl shareControl].personDS.uinfo.needcheck == 1 ? NO:YES;
                [cell setAccessoryView:CheckSwitch];
                CheckSwitch = nil;
            }
        }else if(indexPath.row == 1){
            cell.textLabel.text = @"设置密码";
        }
        else if(indexPath.row == 2)
        {
            cell.textLabel.text = @"新消息通知";
        }
    }else{

    }
  return cell;
}

#pragma mark -table delegate-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0) {
        UIActionSheet *sheetPhoto=[[UIActionSheet alloc]initWithTitle:@"设置头像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"相册", nil];
        [sheetPhoto showInView:self.view];
        //[sheetPhoto release];
    }else if (indexPath.section == 1) {
        if (indexPath.row == 0)
        {
            [[ViewManager sharedViewManager] showView:VI_GIFT_STROAGE
                                        withIsRemoveCurV:false
                                        withDirection:VD_RIGHT];
        }
        else if (indexPath.row == 1)
        {
            
        }
        else if (indexPath.row == 2){
            [[ViewManager sharedViewManager] showView:VI_SETNAME
                                     withIsRemoveCurV:false
                                        withDirection:VD_RIGHT];
        }else if (indexPath.row == 3){
            
            [self showSexPicker];
            
        }else if (indexPath.row == 4){
            [self showDataPicker];
        }else if (indexPath.row == 5){
            
            [self showCityPicker];

        }else if (indexPath.row == 6){
            [[ViewManager sharedViewManager] showView:VI_SETSIGN
                                     withIsRemoveCurV:false
                                        withDirection:VD_RIGHT];
        }else if(indexPath.row == 7){
            [[ViewManager sharedViewManager] showView:VI_SETMAIL
                                     withIsRemoveCurV:false
                                        withDirection:VD_RIGHT];
        }
    }else{
        if (indexPath.row == 1) {
            [[ViewManager sharedViewManager] showView:VI_SETPS
                                     withIsRemoveCurV:false
                                        withDirection:VD_RIGHT];
        }
        else if(indexPath.row == 2)
        {
            [[ViewManager sharedViewManager] showView:VI_SET_NEWS
                                     withIsRemoveCurV:false
                                        withDirection:VD_RIGHT];
        }
    }
}

#pragma mark -private fun -
-(void)setLabelText
{
    [mtablev reloadData];
}

-(void)initUI
{
    [self createTitle];
    self.mtitle.text = @"个人信息";
    
    UIBarButtonItem *backItem = [self createBarBackButtonItemWithTitle:@"返回" target:self action:@selector(backButtonPressed)];
    self.navItem.leftBarButtonItem = backItem;
    
    mtablev =[[UITableView alloc]initWithFrame:CGRectMake(0, TOPBAR_HIGHT, WINDOW_WIDTH, WINDOW_HIGTH - TOPBAR_HIGHT - 20)
                                        style:UITableViewStyleGrouped];
    mtablev.backgroundColor = CELL_BG_COLOR;
    mtablev.separatorStyle=UITableViewCellSeparatorStyleNone;
    mtablev.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bgColor.png"]];
    mtablev.backgroundView = nil;
    mtablev.delegate =self;
    mtablev.dataSource =self;
    [self.view addSubview:mtablev];
    
    CGRect frame = [[UIScreen mainScreen] bounds];
    frame.origin.y = [[UIScreen mainScreen] bounds].size.height;
    windowPickerView = [[UIView alloc]initWithFrame:frame];
    windowPickerView.backgroundColor = [UIColor colorWithRed:(40/255.0f) green:(40/255.0f) blue:(40/255.0f) alpha:0.6f];
    [self.view addSubview:windowPickerView];
    
    [self initSexPickerView];
    [self initCityPickerView];
    [self initAgePickerView];
}

-(void)backButtonPressed
{
    [[ViewManager sharedViewManager] backView:self];
    [[ViewManager sharedViewManager] refreshSetView];
}

-(void)IsModifySucess:(bool)bsuccess
{
    
    if (bsuccess) {
        switch ([DataControl shareControl].iUUT) {
            case UUT_HEADIMAGE:
            {
                [[ViewManager sharedViewManager] showSucessHUDWithStatus:@"头像上传成功"
                 withDelay:3
                                                                  withView:self.view];
                
                [[DataControl shareControl] UpdateUserImage];
                
                
                [[DataControl shareControl].taskDS CompleteTaskByID:TI_F_CHANGEHEAD
                 withIsDirectShow:true];
            }
                break;
            case UUT_PS:
            {
                [[ViewManager sharedViewManager] showSucessHUDWithStatus:@"密码修改成功"
                 withDelay:3
                                                                         withView:self.view];
                
                [DataControl shareControl].personDS.uinfo.ps = [DataControl shareControl].strChangeNewPS;
                
                [[DataControl shareControl].LCDS addLogicCommand:LC_UPDATEUIDB];
            }
                break;
            default:
                break;
        }
    }
    else
    {
        switch ([DataControl shareControl].iUUT) {
            case UUT_HEADIMAGE:
            {
                [[ViewManager sharedViewManager] showFailHUDWithStatus:@"头像上传失败"
                 withDelay:3
                                                                         withView:self.view];
            }
                break;
                case UUT_PS:
            {
                [[ViewManager sharedViewManager] showFailHUDWithStatus: 
                 @"亲,密码修改失败,请检查输入数据"
                 withDelay:3
                                                              withView:self.view];
            }
                break;
            default:
                break;
        }
    }
    
    [mtablev reloadData];
}

- (void) CheckswitchValueChanged:(id)sender{
    UISwitch* control = (UISwitch*)sender;
    char mcheck = 1;
    if(control.on)
    {
        mcheck = 2;
    }
    else
    {
        mcheck = 1;
    }
    
    [[DataControl shareControl] SetUpdateUserType:UUT_CHECK];
    [[DataControl shareControl] SetChangeCheck:mcheck];
    
    [[DataControl shareControl].LCDS addLogicCommand:LC_SETCHECK];
    
    [DataControl shareControl].personDS.uinfo.needcheck = [DataControl shareControl].ichangeNeedCheck;
    [[DataControl shareControl].LCDS addLogicCommand:LC_UPDATEUIDB];

}

-(void)viewWillShow
{
    [[netManager sharedNetManager].sendgiftStroage sendGiftGetTheStroage:
     [DataControl shareControl].personDS.uinfo.mail];
    
    [[netManager sharedNetManager].sendPersonRC sendReadCoin];
    
    [[DataControl shareControl].LCDS addLogicCommand:LC_SENDPERSONINFO];
    
    iprovice = [DataControl shareControl].personDS.uinfo.province;
    icity = [DataControl shareControl].personDS.uinfo.city;
    
    [mtablev reloadData];
}

-(void)refreshView
{
    [mtablev reloadData];
}

-(void)initSexPickerView
{
    mSexPickerBgView = [[UIView alloc] initWithFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height, 320, 260 + 44)];
    UIBarButtonItem* cancel = [[UIBarButtonItem alloc]
                               initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                               target:self action:@selector(dismissSexPicker)];
    UIBarButtonItem* save = [[UIBarButtonItem alloc]
                             initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                             target:self action:@selector(saveSexPicker)];
    UIBarButtonItem	*Space = [[UIBarButtonItem alloc]
                              initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                              target:self action:nil];
    NSArray* buttons = [NSArray arrayWithObjects:cancel,Space,Space,save,nil];
    //为子视图构造工具栏
    UIToolbar *pickerToolbar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 24, 320, 44)];
    pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
    [pickerToolbar sizeToFit];
    [pickerToolbar setItems:buttons animated:YES]; //把按钮加入工具栏
    [mSexPickerBgView addSubview:pickerToolbar];//把工具栏加入子视图
    
    mSexPV = [[UIPickerView alloc] init];
    [mSexPV setFrame:CGRectMake(0, 68, mSexPV.frame.size.width, mSexPV.frame.size.height)];
    mSexPV.showsSelectionIndicator = YES;
    mSexPV.delegate = self;
    mSexPV.dataSource = self;
    [mSexPickerBgView addSubview:mSexPV];
    
    [self.view addSubview:mSexPickerBgView];
}

- (void)showSexPicker
{
    [windowPickerView setFrame:[[UIScreen mainScreen] bounds]];
    [UIView beginAnimations:nil context:UIGraphicsGetCurrentContext()];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.4];
    mSexPickerBgView.center = CGPointMake(mSexPickerBgView.center.x, [[UIScreen mainScreen] bounds].size.height - mSexPickerBgView.frame.size.height/2);
    [UIView commitAnimations];
}

- (void)dismissSexPicker {
    
    CGRect frame = [[UIScreen mainScreen] bounds];
    frame.origin.y = [[UIScreen mainScreen] bounds].size.height;
    windowPickerView.frame = frame;
    
    [UIView beginAnimations:nil context:UIGraphicsGetCurrentContext()];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.4];
    mSexPickerBgView.center = CGPointMake(mSexPickerBgView.center.x, [[UIScreen mainScreen] bounds].size.height + mSexPickerBgView.center.y);
    [UIView commitAnimations];
}

-(void)saveSexPicker
{
    int isex = [mSexPV selectedRowInComponent:0];
    
    [DataControl shareControl].personDS.uinfo.sex = (isex == 0)? 2:1;
    
    [[DataControl shareControl] SetUpdateUserType:UUT_SEX];
    [[DataControl shareControl].LCDS addLogicCommand: LC_UPDATEUIDB];
    
    [[DataControl shareControl] SetChangeSex:[DataControl shareControl].personDS.uinfo.sex];
    [[DataControl shareControl].LCDS addLogicCommand:LC_SETSEX];
    
    [self dismissSexPicker];
    
    [mtablev reloadData];
}

- (void)initCityPickerView
{
    mCityPickerBgView = [[UIView alloc] initWithFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height, 320, 260 + 44)];
    UIBarButtonItem* cancel = [[UIBarButtonItem alloc]
                               initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                               target:self action:@selector(dismissCityPicker)];
    UIBarButtonItem* save = [[UIBarButtonItem alloc]
                             initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                             target:self action:@selector(saveCityPicker:)];
    UIBarButtonItem	*Space = [[UIBarButtonItem alloc]
                              initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                              target:self action:nil];
    NSArray* buttons = [NSArray arrayWithObjects:cancel,Space,Space,save,nil];
    //为子视图构造工具栏
    UIToolbar *pickerToolbar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 24, 320, 44)];
    pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
    [pickerToolbar sizeToFit];
    [pickerToolbar setItems:buttons animated:YES]; //把按钮加入工具栏
    [mCityPickerBgView addSubview:pickerToolbar];//把工具栏加入子视图
    
    mCityPV = [[UIPickerView alloc] init];
    [mCityPV setFrame:CGRectMake(0, 68, mCityPV.frame.size.width, mCityPV.frame.size.height)];
    mCityPV.showsSelectionIndicator = YES;
    mCityPV.delegate = self;
    mCityPV.dataSource = self;
    [mCityPickerBgView addSubview:mCityPV];
    
    [self.view addSubview:mCityPickerBgView];
}

- (void)showCityPicker {
    
    [mCityPV selectRow:iprovice inComponent:0 animated:NO];
    [mCityPV selectRow:icity inComponent:1 animated:NO];
    [windowPickerView setFrame:[[UIScreen mainScreen] bounds]];
    [UIView beginAnimations:nil context:UIGraphicsGetCurrentContext()];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.4];
    mCityPickerBgView.center = CGPointMake(mCityPickerBgView.center.x, [[UIScreen mainScreen] bounds].size.height - mCityPickerBgView.frame.size.height/2);
    [UIView commitAnimations];
}

- (void)dismissCityPicker {
    
    CGRect frame = [[UIScreen mainScreen] bounds];
    frame.origin.y = [[UIScreen mainScreen] bounds].size.height;
    windowPickerView.frame = frame;
    
    [UIView beginAnimations:nil context:UIGraphicsGetCurrentContext()];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.4];
    mCityPickerBgView.center = CGPointMake(mCityPickerBgView.center.x, [[UIScreen mainScreen] bounds].size.height + mCityPickerBgView.center.y);
    [UIView commitAnimations];
}

- (void)saveCityPicker:(id)sender {
    
    iprovice = [mCityPV selectedRowInComponent:0];
    icity = [mCityPV selectedRowInComponent:1];
    
    [DataControl shareControl].personDS.uinfo.province = iprovice;
    [DataControl shareControl].personDS.uinfo.city = icity;
    
    [[DataControl shareControl] SetUpdateUserType:UUT_PROVINCE];
    [[DataControl shareControl].LCDS addLogicCommand: LC_UPDATEUIDB];
    
    [[DataControl shareControl].personDS SetChagePC:iprovice withCity:icity];
    [[DataControl shareControl].LCDS addLogicCommand:LC_SETPC];
    
    [self dismissCityPicker];
    
    [mtablev reloadData];
}

- (void)initAgePickerView
{
        
        //默认日期格式为yyyy-MM-dd
        NSDateFormatter *dateFormatter= [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSDate *birthdayData = [dateFormatter dateFromString: @"1990-01-01 09:00:00"];
        
        datePicker = [[UIDatePicker alloc]init];
        [datePicker setDate:birthdayData];
        datePicker.frame = CGRectMake(0, 88, 320, 260);
        datePicker.datePickerMode = UIDatePickerModeDate;
        
        datePicker.maximumDate = [dateFormatter dateFromString:@"2000-01-01 09:00:00"];
        datePicker.minimumDate = [dateFormatter dateFromString:@"1900-01-01 09:00:00"];
        
        //构造一个子视图,用于显示日期选择器
        pickerBgView = [[UIView alloc]initWithFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height, 320, 320)];
        pickerBgView.backgroundColor = [UIColor clearColor];
        //为子视图构造工具栏按钮
        UIBarButtonItem* cancel = [[UIBarButtonItem alloc]
                                   initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                                   target:self action:@selector(dismissDataPicker)];
        UIBarButtonItem* save = [[UIBarButtonItem alloc]
                                 initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                 target:self action:@selector(saveDataPicker)];
        UIBarButtonItem	*Space = [[UIBarButtonItem alloc]
                                  initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                  target:self action:nil];
        NSArray* buttons = [NSArray arrayWithObjects:cancel,Space,Space,save,nil];
        //为子视图构造工具栏
        UIToolbar *pickerToolbar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 44, 320, 44)];
        pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
        [pickerToolbar sizeToFit];
        [pickerToolbar setItems:buttons animated:YES]; //把按钮加入工具栏
        [pickerBgView addSubview:pickerToolbar];//把工具栏加入子视图
        
        [pickerBgView addSubview:datePicker]; //把datePicker加入子视图
        
        [self.view addSubview:pickerBgView];
        [self.view bringSubviewToFront:pickerBgView];
}

- (void)showDataPicker
{
    [windowPickerView setFrame:CGRectMake(0, 0, windowPickerView.frame.size.width, windowPickerView.frame.size.height)];
    
    if(pickerBgView != nil){
        [UIView beginAnimations:nil context:UIGraphicsGetCurrentContext()];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDuration:0.4];
        pickerBgView.center = CGPointMake(pickerBgView.center.x, [[UIScreen mainScreen] bounds].size.height - pickerBgView.frame.size.height/2);
        [UIView commitAnimations];
    }
}

- (void)dismissDataPicker
{
    CGRect frame = [[UIScreen mainScreen] bounds];
    frame.origin.y = [[UIScreen mainScreen] bounds].size.height;
    windowPickerView.frame = frame;
    
    if(pickerBgView != nil){
        [UIView beginAnimations:nil context:UIGraphicsGetCurrentContext()];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDuration:0.4];
        pickerBgView.center = CGPointMake(pickerBgView.center.x, [[UIScreen mainScreen] bounds].size.height + pickerBgView.center.y);
        [UIView commitAnimations];
    }
}

- (void)saveDataPicker
{
    [self dateChange];
    
    [mtablev reloadData];

    [self dismissDataPicker];
}

-(void)dateChange
{
    NSDate* date = datePicker.date;
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSYearCalendarUnit |
    NSMonthCalendarUnit |
    NSDayCalendarUnit |
    NSWeekdayCalendarUnit |
    NSHourCalendarUnit |
    NSMinuteCalendarUnit |
    NSSecondCalendarUnit;
    comps = [calendar components:unitFlags fromDate:date];
    int year=[comps year];
    
    NSDate *datenow = [NSDate date];
    NSCalendar *calendarnow = [NSCalendar currentCalendar];
    NSDateComponents *compsnow;
    compsnow =[calendarnow components:(NSYearCalendarUnit | NSMonthCalendarUnit |NSDayCalendarUnit)
                             fromDate:datenow];
    if ([compsnow month] <= [comps month]) {
        if ([compsnow month] == [comps month])
        {
            if ([compsnow day] < [comps day])
            {
                year = [compsnow year] - year - 1;
            }
            else
            {
                year = [compsnow year] - year;
            }
        }
        else
        {
            year = [compsnow year] - year - 1;
        }
    }
    else
    {
        year = [compsnow year] - year;
    }
    
    [DataControl shareControl].personDS.uinfo.age = year;
    
    [[DataControl shareControl] SetUpdateUserType:UUT_AGE];
    [[DataControl shareControl].LCDS addLogicCommand:LC_UPDATEUIDB];
    
    [[DataControl shareControl] SetChangeAge:year];
    [[DataControl shareControl].LCDS addLogicCommand:LC_SETAGE];

}

#pragma mark UIActionSheetDelegate -
-(void)actionSheetCancel:(UIActionSheet *)actionSheet
{
}

-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
    switch (buttonIndex) {
        case 0:{
            //照相机
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                UIImagePickerController *picker=[[UIImagePickerController alloc]init];
                picker.sourceType=UIImagePickerControllerSourceTypeCamera;
                picker.allowsEditing = YES;
                picker.delegate=self;
                [self presentViewController:picker animated:YES completion:nil];
            }
        }
            break;
        case 1:{
            //相片库
            UIImagePickerController *picker=[[UIImagePickerController alloc]init];
            picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
            picker.allowsEditing=YES;
            picker.delegate=self;
            [self presentViewController:picker animated:YES completion:nil];
        }
            break;
        case 2:
            //取消
            break;
        default:
            break;
    }
}

#pragma mark UIImagePickerControllerDelegate -
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
    [picker dismissModalViewControllerAnimated:YES];
    
    [[DataControl shareControl] setHeadImage:[image transformWidth:SMALL_HEAD_IMAGE
                                                            height:SMALL_HEAD_IMAGE]];
    [[DataControl shareControl].bigHIDS SetUserBigImage:[image transformWidth:BIG_HEAD_IMAGE
                                                               height:BIG_HEAD_IMAGE]];
    [[DataControl shareControl].LCDS addLogicCommand:LC_SETHEADIMAGE];
    
    [[ViewManager sharedViewManager] showSuccessProgressHUDWithStatus:@"上传头像"
                                                             withView:self.view];
    [[DataControl shareControl] SetUpdateUserType:UUT_HEADIMAGE];
}
     
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
    //[picker release];
}

#pragma mark- UIPickerView Data Source And Delegate 
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    if (pickerView == mSexPV)
    {
        return 1;
    }
    else if(pickerView == mCityPV)
    {
        return 2;
    }
    
    return 2;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
    if (pickerView == mSexPV) {
        return 2;
    }
    else if(pickerView == mCityPV)
    {
        if(0==component)
            return [DataControl shareControl].mapArray.count;
        else
        {
            return [[[[DataControl shareControl].mapArray objectAtIndex:
                      [pickerView selectedRowInComponent:0]] objectForKey:@"Cities"]count];
        }

    }
    
    return 1;
}
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    if (pickerView == mSexPV)
    {
        return 320;
    }
    else if(pickerView == mCityPV)
    {
        return 150;
    }
    
    return 320;
}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 40;
}

-(UIView*)pickerView:(UIPickerView *)pickerView
          viewForRow:(NSInteger)row forComponent:(NSInteger)component
         reusingView:(UIView *)view
{
    CGFloat width=[self pickerView:pickerView widthForComponent:component];
    CGFloat height=[self pickerView:pickerView rowHeightForComponent:component];
    UILabel* label;
    if(nil==view)
    {
        label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, height)];
        //[label autorelease];
    }
    else
        label=(UILabel*)view;
    
    label.backgroundColor = [UIColor clearColor];
    label.font=[UIFont fontWithName:@"Arial" size:20];
    label.textAlignment=NSTextAlignmentCenter;
    
    if (pickerView == mSexPV) {
        if(0==row)
        {
            label.text = @"女";
        }
        else
        {
            label.text = @"男";
        }

    }
    else if(pickerView == mCityPV)
    {
        if(0==component)
        {
            label.text = getProvinceFromMapArr(row);
        }
        else
        {
            iprovice = [pickerView selectedRowInComponent:0];
            label.text = getCityFromMapArr(iprovice, row);
        }
    }
    
    return label;
}

-(void)pickerView:(UIPickerView *)pickerView
     didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (pickerView == mCityPV)
    {
        if(0==component)
        {
            [pickerView reloadComponent:1];
            [pickerView selectRow:0 inComponent:1 animated:NO];
        }
        iprovice = [pickerView selectedRowInComponent:0];
        icity = [pickerView selectedRowInComponent:1];
    }
}

@end
